package org.gap.wizards;

import java.util.ArrayList;
import java.util.List;

import org.gap.wizards.state.TransitionState;
import org.gap.wizards.views.View;
import org.gap.wizards.views.ViewPlatform;
import org.gap.wizards.world.MapContainer;
import org.gap.wizards.world.Movement;
import org.gap.wizards.world.TimerState;
import org.gap.wizards.world.actions.Factory;

public class InGameState implements TransitionState {
	public static final char UP = 'k';
	public static final char DOWN = 'j';
	public static final char LEFT = 'h';
	public static final char RIGHT = 'l';
	public static final char UP_LEFT = 'y';
	public static final char UP_RIGHT = 'u';
	public static final char DOWN_RIGHT = 'n';
	public static final char DOWN_LEFT = 'b';
	public static final char TRANSITION = 'x';
	
	private final MapContainer container;
	private final List<View> drawables;
	private final TimerState timer;

	public InGameState(MapContainer container, TimerState timer) {
		this.container = container;
		this.timer = timer;
		drawables = new ArrayList<View>(10);
	}

	@Override
	public void acceptKey(char keyPress) {
		if (Movement.isValid(keyPress)) {
			Movement direction = Movement.from(keyPress);
			container.move(direction);
		} else if (Factory.accepts(keyPress)) {
			Factory action = Factory.from(keyPress);
			container.perform(action);
		}
		timer.tick();
	}
	
	public void addDrawableView(View drawable) {
		drawables.add(drawable);
	}

	@Override
	public void draw(ViewPlatform console) {
		container.draw(console);
		for (View each : drawables) {
			each.draw();
		}
	}

	@Override
	public void enter() {

	}

	@Override
	public void exit() {

	}
}
